package com.gaohe.dcp.service.impl;

import java.util.List;

import com.gaohe.common.core.domain.entity.SysRole;
import com.gaohe.common.core.domain.entity.SysUser;
import com.gaohe.common.utils.DateUtils;
import com.gaohe.common.utils.SecurityUtils;
import com.gaohe.dcp.domain.DcpScaleScreen;
import com.gaohe.dcp.mapper.DcpScaleScreenMapper;
import com.gaohe.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gaohe.dcp.mapper.DcpPatientMapper;
import com.gaohe.dcp.domain.DcpPatient;
import com.gaohe.dcp.service.IDcpPatientService;

/**
 * 病患管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@Service
public class DcpPatientServiceImpl implements IDcpPatientService
{
    @Autowired
    private DcpPatientMapper dcpPatientMapper;
    @Autowired
    private DcpScaleScreenMapper dcpScaleScreenMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public int updateDoctor(DcpPatient dcpPatient)
    {
        return dcpPatientMapper.updateDoctor(dcpPatient);
    }
    /**
     * 查询病患管理
     *
     * @param patientId 病患管理主键
     * @return 病患管理
     */
    @Override
    public DcpPatient selectDcpPatientByPatientId(Long patientId)
    {
        return dcpPatientMapper.selectDcpPatientByPatientId(patientId);
    }

    /**
     * 查询病患管理列表
     *
     * @param dcpPatient 病患管理
     * @return 病患管理
     */
    @Override
    public List<DcpPatient> selectDcpPatientList(DcpPatient dcpPatient)
    {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        List<SysRole> roles = sysUser.getRoles();
        Long roleId = roles.get(0).getRoleId();
        if (roleId == 2){
            //医院负责人
            System.err.println("医院负责人查询全部");
            return dcpPatientMapper.selectDcpPatientList2(dcpPatient);
        }else {
            //其他 医生
            System.err.println("医生查看自己");
            List<DcpPatient> dcpPatients = dcpPatientMapper.selectDcpPatientList(dcpPatient);
            System.err.println(dcpPatients.size());
            return dcpPatients;
        }

    }

    /**
     * 新增病患管理
     *
     * @param dcpPatient 病患管理
     * @return 结果
     */
    @Override
    public int insertDcpPatient(DcpPatient dcpPatient)
    {
        dcpPatient.setCreateTime(DateUtils.getNowDate());
        return dcpPatientMapper.insertDcpPatient(dcpPatient);
    }

    /**
     * 修改病患管理
     *
     * @param dcpPatient 病患管理
     * @return 结果
     */
    @Override
    public int updateDcpPatient(DcpPatient dcpPatient)
    {
        dcpPatient.setUpdateTime(DateUtils.getNowDate());
        return dcpPatientMapper.updateDcpPatient(dcpPatient);
    }

    /**
     * 批量删除病患管理
     *
     * @param patientIds 需要删除的病患管理主键
     * @return 结果
     */
    @Override
    public int deleteDcpPatientByPatientIds(Long[] patientIds)
    {
        return dcpPatientMapper.deleteDcpPatientByPatientIds(patientIds);
    }

    /**
     * 删除病患管理信息
     *
     * @param patientId 病患管理主键
     * @return 结果
     */
    @Override
    public int deleteDcpPatientByPatientId(Long patientId)
    {
        return dcpPatientMapper.deleteDcpPatientByPatientId(patientId);
    }

    @Override
    public int insertDcpPatients(List<DcpScaleScreen> dcpScaleScreen) {
        Long patientId = dcpScaleScreen.get(0).getPatientId();
        dcpScaleScreenMapper.deleteDcpScaleScreenByScalePatientId(patientId);
        for (DcpScaleScreen d: dcpScaleScreen){
            dcpScaleScreenMapper.insertDcpScaleScreen(d);
        }
        return 1;
    }

    @Override
    public List<DcpPatient> getDcpPatientList() {
        return dcpPatientMapper.getDcpPatientList();
    }

    @Override
    public List<DcpPatient> selectDcpPatient(DcpPatient dcpPatient) {
        return dcpPatientMapper.selectDcpPatient(dcpPatient);
    }
}
